﻿
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace wpf.Triage.Convert
{
    public class MultCheckBoxConvert2 : IValueConverter
    {
        object OldValue = null;
        /// <summary>
        /// 将
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (value == null)
                {
                    if (OldValue != null)
                    {
                        ChckedList.Clear();
                    }
                    OldValue = value;
                    return false;
                }
                string str1 = value.ToString();
                string str2 = parameter.ToString();
                if (!value.Equals(OldValue))
                {
                    ChckedList = str1.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                }
                OldValue = value;
                if (str1.Contains(str2))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        public List<string> ChckedList = new List<string>();
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool bl = false;

            if (value is bool)
            {
                bl = bool.Parse(value.ToString());
                if (parameter != null)
                {
                    string parm = parameter.ToString();
                    if (bl)
                    {
                        if (!ChckedList.Contains(parm))
                        {
                            if (parm.Equals("无"))
                            {
                                if (ChckedList.Count == 0)
                                {
                                    ChckedList.Add(parm);
                                }
                            }
                            else
                            {
                                if (!ChckedList.Contains("无"))
                                {
                                    ChckedList.Add(parm);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ChckedList.Contains(parm))
                        {
                            ChckedList.Remove(parm);
                        }
                    }
                }
            }

            var str = string.Join(",", ChckedList);
            return str;
        }
    }
}
